// ----------------------------------
// RSDK Project: Sonic Pocket Reventure
// Script Description: [Object Name]
// Script Author: [YOU]
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : boss.timer
private alias object.value1 : shake.timer
private alias object.value2 : boss.hitCount
private alias object.value3 : boss.cooldown
//-------Function declarations-------//

//-------Static Values-------//

//-------Tables-------//


//-------Functions-------//


event ObjectMain
		object.priority = PRIORITY_ACTIVE
		object[+1].type = TypeName[Curb Chain]
		temp0 = object.xpos
		if object.direction == 0
			temp0 -= 0x10000
		else
			temp0 += 0x10000
		endif
		temp1 = object.ypos
		temp1 += 0x270000
		object[+1].xpos = temp0
		object[+1].ypos = temp1

		object[+2].type = TypeName[Exhaust]
		temp0 = object.xpos
		if object.direction == 0
			temp0 += 0x190000
		else
			temp0 -= 0x190000
		endif
		temp1 = object.ypos
		temp1 += 0xE0000
		object[+2].xpos = temp0
		object[+2].ypos = temp1
	switch object.state
	case 0
		temp0 = object[-1].ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object[-1].ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		if object[0].XPos > object[-1].xpos
			ResetObjectEntity(26, TypeName[Music Event], 3, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state ++
			boss.hitCount = 8
		endif
		break
	case 1
		if boss.timer > 59
			object.ypos +=0x20000
			shake.timer = 64
		else
			boss.timer++
		endif
		break
	case 2
		if shake.timer > 0
			shake.timer--
			temp0 = shake.timer
			temp0 &= 7
			if temp0 == 0
				screen.shakeX = 1
			end if
			temp0 = shake.timer
			temp0 &= 3
			if temp0 == 0
				screen.shakeY = 2
			end if
		else
			if boss.timer > 29
				object.state ++
				boss.timer = 0
			else
				boss.timer++
			endif
		end if
		break
	case 3
		if object.direction == 0
			object.xpos -= 0x18000
		else
			object.xpos += 0x18000
		endif

		temp0 = stage.curXBoundary1
		temp0 -= 64
		temp1 = stage.curXBoundary2
		temp1 += 64

		if object.ixpos < temp0
			object.state ++
		endif
		if object.ixpos > temp1
			object.state ++
		endif
		break
	case 4
		if boss.timer > 5
			object.state = 3
			object.direction ^=1
			object[+1].direction ^=1
			object[+2].direction ^=1
			boss.timer = 0
		else
			boss.timer++
		endif
		break
	case 12
		object.value23++
		temp0 = object.value23
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 32)
			temp0 -= 16
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], 0)
		end if
		if object.value23 == 180
			object.animation = 5
			object.animationTimer = 0
			object.value23 = 0
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 13
		break
	endswitch

	ObjectTileCollision(CSIDE_FLOOR, 0, 46, 0)
	if checkResult == 1
		object.state = 2
		boss.timer = 0
	end if
	if boss.hitCount != 0
		if boss.cooldown > 0
			boss.cooldown--
			GetBit(temp0, boss.cooldown, 0)
			if temp0 == 1
				SetPaletteEntry(0, 128, 0xE0E0E0)
				SetPaletteEntry(1, 128, 0xE0E0E0)
			else
				SetPaletteEntry(0, 128, 0)
				SetPaletteEntry(1, 128, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if boss.cooldown == 0
				if object.state == 3
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 18, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(PlayerObject_CheckHit)
						if checkResult == 1
							//FlipSign(object[currentPlayer].xvel)
							boss.hitCount--	
							if boss.hitCount == 0
								player.score += 1000
								stage.timeEnabled = false
								if PlayerObject_SuperState != 0
									PlayerObject_SuperState = 2
								end if
								object.state = 12
							else
								boss.cooldown = 32
								PlaySfx(SfxName[Boss Hit], 0)
							endif
						endif
					end if
				end if
			end if
		next
	end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				temp0 += object[currentPlayer].xpos
				temp1 = stage.curXBoundary2
				temp1 <<= 16
				if temp0 > temp1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
					object[currentPlayer].xpos = temp1
					temp0 = object[currentPlayer].collisionRight
					temp0 <<= 16
					object[currentPlayer].xpos -= temp0
				end if
		next
end event

event ObjectDraw

	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)

	temp0 =	screen.xcenter
	temp0 -= 64
	temp1 = screen.ysize
	temp1 -= 16
	if object.state > 0
		DrawSpriteScreenXY(7, temp0, temp1)
	endif
	temp0 += 16
	temp1 -= 4
	temp2 = boss.hitCount
	temp2 *= 16
	if object.state > 0
		DrawRect( temp0, temp1, temp2, 8, 255, 0, 0, 255
	endif


end event

event ObjectStartup
    	LoadSpriteSheet("NSZ/Objects.gif")
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//0 Eggman Idle
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//1 Eggman
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//2 Eggman
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//3 Eggman
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//4 Eggman
	SpriteFrame(-30, -10, 60, 20, 1, 1)	//5 Eggman

	SpriteFrame(-46, 10, 76, 29, 1, 64)	//6 Eggman Body	
	SpriteFrame(-9, -8, 18, 16, 172, 1)	//7 Boss Bar Eggman	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 223, 172)
end event